<?php
// 产品
namespace app\api\controller;
use app\api\model\Product as productModel;
use app\admin\model\warehouse\Productspec;
use think\facade\Config;

class Product extends LoginController
{
    /**
     * @product_list   总库列表
     * @param   接口参数
     * @param.page              页码
     * @param.product_status    产品状态筛选
     */
    public function product_list(){
        
        $data = $this->request->param();
        $whereis = [];
        // 页码
        $page = 0;
        if (!empty($data['page'])) {
            $page = $data['page'];
        }
        // 产品状态筛选
        if (!empty($data['product_status'])) {
            $whereis = [
                ['product_status', '=', $data['product_status']]
            ];
        }
        // 产品名称
        if (!empty($data['product_name'])) {
            $whereis = [
                ['product_name', 'like', '%'.$data['product_name'].'%' ]
            ];
        }
        // 产品编码
        if (!empty($data['product_sku'])) {
            $whereis = [
                ['product_sku', 'like', '%'.$data['product_sku' ].'%' ]
            ];
        }

        // print_r($whereis); exit();
        $productModel = new productModel;
        
        $count = $productModel
            ->withJoin('productSpec', 'LEFT')
            ->where($whereis)
            ->count();
        $list = $productModel
            ->withJoin('productSpec', 'LEFT')
            ->where($whereis)
            ->page($page, $this->limit_page)
            // ->order($this->sort)
            ->select();
        // 如果list为空 返回自定义报错
        if (!$list) {
            $result = [
                'code'  => -1,
                'msg'   => '暂无成品库数据!',
            ];
        } else {

            //序列化数组
            foreach ($list as &$value) {
                $value->product_status_text = Config::get('app')['const_data']['product_status_list'][$value->product_status];
            }
            $result = [
                'code'  => 1,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
        }
        
        return json($result);
    }

    /**
     * @product_info   成品库详情
     * @param   接口参数
     * @param.product_id    产品ID
     */
    public function product_info(){
        
        $data = $this->request->param();
        $whereis = [];
        // 产品状态筛选
        if (empty($data['product_id'])) {
            return json([
                'code'  => -1,
                'msg'   => '缺少成品库ID!',
            ]);
        }

        $productModel = new productModel;
        $row = $productModel
            ->withJoin('productSpec', 'LEFT')
            ->where('ea_system_product.id',$data['product_id'])
            ->find();
        // 如果list为空 返回自定义报错
        if (!$row) {
            $result = [
                'code'  => -1,
                'msg'   => '暂无成品库数据!',
            ];
        } else {

            //序列化数组
            $row->product_status_text = Config::get('app')['const_data']['product_status_list'][$row->product_status];

            $result = [
                'code'  => 1,
                'msg'   => '',
                'data'  => $row,
            ];
        }
        
        return json($result);
    }

    /**
     * @product_recovery_add   成品库_添加回收
     * @param                  接口参数
     * @param.product_name     产品名称
     * @param.product_spec_id  规格ID
     * @param.product_sku      编号
     * @param.remark           备注
     */
    public function product_recovery_add(){
        $data = $this->request->param();

        if (empty($data['product_name'])) {
            return json([
                'code'  => -1,
                'msg'   => '缺少产品名称!',
            ]);
        }
        if (empty($data['product_spec_id'])) {
            return json([
                'code'  => -1,
                'msg'   => '缺少产品编号!',
            ]);
        }
        if (empty($data['product_sku'])) {
            return json([
                'code'  => -1,
                'msg'   => '缺少产品编号!',
            ]);
        }
        // if (empty($data['remark'])) {
        //     return json([
        //         'code'  => -1,
        //         'msg'   => '缺少产品备注!',
        //     ]);
        // }

        $arr['product_status'] = 4; //产品状态
        $arr['product_name'] = $data['product_name'];   //产品名称
        $arr['product_spec_id'] = $data['product_spec_id']; //规格ID
        $arr['product_sku'] = $data['product_sku']; //产品编码
        $arr['remark'] = $data['remark']?? '';  //备注
        $arr['product_create_time'] = time();   //添加时间

        $productModel = new productModel;
        try {
            $productModel->save($arr);
            $result = [
                'code'  => 1,
                'msg'   => '产品添加成功!',
            ];
        } catch (\Exception $e) {
            $result = [
                'code'  => -1,
                'msg'   => '产品添加失败!',
            ];
        }

        return json($result);
    }

    /**
     * @product_edit   成品库_产品修改
     * @param                  接口参数
     * @param.product_id       产品ID
     * @param.product_name     产品名称
     * @param.product_spec_id  规格ID
     * @param.product_sku      编号
     * @param.remark           备注
     */
    public function product_edit(){
        $data = $this->request->param();

        if (empty($data['product_id'])) {
            return json([
                'code'  => -1,
                'msg'   => '缺少产品ID!',
            ]);
        }
        $productModel = new productModel;
        $row = $productModel->find($data['product_id']);
        if (!$row) {
            return json([
                'code'  => -1,
                'msg'   => '数据不存在!',
            ]);
        }

        $arr['product_status'] = $data['product_status'];   //产品状态
        $arr['user_id'] = $data['user_id'] ??0;   //用户ID
        $arr['product_update_time'] = time();   //修改时间
        // print_r($arr); exit;    
        try {
            $productModel->where('id',$data['product_id'])->save($arr);
            $result = [
                'code'  => 1,
                'msg'   => '产品修改成功!',
            ];
        } catch (\Exception $e) {
            $result = [
                'code'  => -1,
                'msg'   => '产品修改失败!',
            ];
        }

        return json($result);
    }

    /**
     * @product_spec_list   类别列表
     * @param               接口参数
     * @param.page          页码
     */
    public function product_spec_list(){

        $data = $this->request->param();
        $page = 0;
        if (!empty($data['page'])) {
            $page = $data['page'];
        }

        $productspecModel = new Productspec;
        $count = $productspecModel
                ->count();
        $list = $productspecModel
            ->page($page, $this->limit_page)
            ->select();
        // 如果list为空 返回自定义报错
        if (!$list) {
            $result = [
                'code'  => -1,
                'msg'   => '暂无规格数据!',
            ];
        } else {
            $result = [
                'code'  => 1,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
        }
        return json($result);
    }

    public function test(){
        print_r(Config::get('app')['const_data']);
        exit;
    }

}